package com.kgc.service.impl;

import com.kgc.constants.Constants;
import com.kgc.pojo.SmsLog;
import com.kgc.mapper.SmsLogMapper;
import com.kgc.service.SmsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.utils.redis.RedisKeyUtils;
import com.kgc.utils.redis.RedisUtils;
import com.kgc.utils.sms.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 短信记录 服务实现类
 * </p>
 *
 * @author zsy
 * @since 2021-08-04
 */
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SendSms sendSms;
    @Override
    public void sendMsg(String phone, Integer codeType) {
            Integer status = Constants.Sms.STATUS_SEND_FAILED;
            // 1.验证码
            String sendCode = "0000";
            // 2.调用发送方法获得结果
            String msgResult = sendSms.send(phone, sendCode);
            if (msgResult != null && msgResult.equals("OK")) {
                status = Constants.Sms.STATUS_SEND_SUCCESS;
                // 成功之后将验证码存入redis
                String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS, codeType.toString(), phone);
                System.out.println("---------------------------" + sendCode);
                redisUtils.putValue(key, sendCode, Constants.Duration.HALF_AN_HOUR);
            }
            SmsLog smsLog = new SmsLog(phone, codeType, sendCode, msgResult, status);
            this.save(smsLog);
    }
}
